<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>3.props的简写方式</title>
    <!-- 引入react核心库 -->
    <script src="../React-js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../React-js/react-dom.development.js"
    ></script>
    <!-- 引入babel,用于将jsx转换为js -->
    <script type="text/javascript" src="../React-js/babel.min.js"></script>
    <!-- 引入 prop-types 用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../React-js/prop-types.js"></script>
  </head>
  <body>
    <!-- 准备好一个“容器” -->
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>

    <script type="text/babel">
      // 创建组件
      class Person extends React.Component {
        constructor(props) {
          // 构造器是否接受props，是否传递给super 取决于:是否希望在构造器中通过this访问props
          super(props);
          console.log('constructor', this.props);
        }

        // Person.属性规则   static 静态
        // 对标签属性进行类型,必要性的限制
        static propTypes = {
          name: PropTypes.string.isRequired,
          sex: PropTypes.string,
          age: PropTypes.number,
        };
        // 指定标签的默认属性值
        static defaultProps = {
          sex: '女--默认',
          age: 22,
        };

        render() {
          // console.log(this);
          const { name, sex, age } = this.props;
          // props 是只读的
          return (
            <ul>
              <li>姓名:{name}</li>
              <li>性别:{sex}</li>
              <li>年龄:{age + 1}</li>
            </ul>
          );
        }
      }

      // 2.渲染组件到页面
      ReactDOM.render(<Person name="哈哈" />, document.getElementById('test1'));
    </script>
  </body>
</html>
